home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snpd1292.zip / LOG.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  8KB  |  217 lines

  1. /************************************************************************
  2. * LOG.C - Creates a ASCII file with time and date stamps for logging    *
  3. *         hours.                                                        *
  4. *                                                                       *
  5. *   usage: LOG [IN][OUT][CALC]                                          *
  6. *               IN - Creates an opening entry from which a "time spent" *
  7. *                    is calculated.                                     *
  8. *              OUT - Creates a closing entry and calculates             *
  9. *                    "time spent" for that entry.                       *
  10. *             CALC - Calculates total overall "time spent" for the      *
  11. *                    entire log.                                        *
  12. *                                                                       *
  13. *   NOTES:  I used seconds to do all the calculations. The other        *
  14. *           time/date entries are for human readability. Some           *
  15. *           enhancments can be done to this program.                    *
  16. *           i.e. Wage/Pay calculation, closing the log after a CALC     *
  17. *           to insure log is not reused, tracking hours for individual  *
  18. *           people, tracking hours for individual projects, etc.        *
  19. *                                                                       *
  20. *  Public domain by Robert Sprawls.                                     *
  21. ************************************************************************/
  22.  
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <stddef.h>
  26. #include <string.h>
  27. #include <time.h>
  28.  
  29. /* Define time file constants   */
  30.  
  31. #define HOUR        3600        /* Number of seconds in an hour.        */
  32. #define MINS        60          /* Number of seconds in a minute        */
  33. #define IN_ENTRY    40          /* Size of an IN entry                  */
  34. #define SEC_OFF     4           /* Offset of seconds in an IN entry     */
  35. #define HOUR_OFF    64          /* Offset of seconds in an "time spent" */
  36.                                 /* calculated entry.                    */
  37.  
  38. /* Define values returned to DOS */
  39.  
  40. #define OK          0   /* Executed normally, nothing will be eched */
  41. #define OPENLOG     1   /* Attempted to log in while open entry in log */
  42. #define CLOSEDLOG   2   /* Attempted to log out while closed entry in log */
  43. #define FILE_ERROR  3   /* File access error. Just in case. */
  44. #define SEEK_ERROR  4   /* File positioning error */
  45. #define NOPARMS     5   /* No parameters supplied to program */
  46. #define INVALID     6   /* Invalid parameters */
  47.  
  48. void    usage( void );
  49. long    get_in_entry( FILE * );
  50. void    fastforw( FILE * );     /* Oppisite of rewind();        */
  51. void    quit( int );
  52.  
  53. char    strbuf[ IN_ENTRY + 1 ];
  54. FILE    *wrklog;
  55.  
  56. int main( int argc, char *argv[] )
  57. {
  58.     char    outline[ IN_ENTRY * 2 + 1 ];
  59.     long    in_entry_time = 0, total_seconds = 0;
  60.     double  hours, pay;
  61.     time_t  current_time;
  62.     div_t   hdiv, mdiv;
  63.  
  64.     if( argc < 2 )
  65.     {
  66.         usage();
  67.         quit( NOPARMS );
  68.     }
  69.  
  70.     /* Open log. Can be any directory.  */
  71.     if(( wrklog = fopen( "WORK.LOG", "a+" )) == NULL )
  72.         quit( FILE_ERROR );
  73.  
  74.     strupr( argv[ 1 ] );
  75.  
  76.     time( ¤t_time );
  77.  
  78.     /* Create an opening IN entry.  */
  79.     if( strcmp( "IN", argv[ 1 ] ) == 0 )
  80.     {
  81.         /* Make sure there isn't a open entry already.  */
  82.         if( get_in_entry( wrklog ) )
  83.             quit( OPENLOG );
  84.  
  85.         /* Stamp it.    */
  86.         fprintf( wrklog, "%3s %ld %s", argv[ 1 ], current_time,
  87.             ctime( ¤t_time ));
  88.     }
  89.     /* Create a closinf OUT entry.  */
  90.     else if( strcmp( "OUT", argv[ 1 ] ) == 0 )
  91.     {
  92.         /* Make sure there is a previous IN entry.  */
  93.         if( ( in_entry_time = get_in_entry( wrklog )) == 0 )
  94.             quit( CLOSEDLOG );
  95.  
  96.         total_seconds = current_time - in_entry_time;
  97.         sprintf( outline, "%3s %ld %s", argv[ 1 ], current_time,
  98.             ctime( ¤t_time ));
  99.  
  100.         /* Cut off the newline character that's normally put on.    */
  101.         outline[ strlen( outline ) - 1 ] = '\0';
  102.         hdiv = div( total_seconds, HOUR );
  103.         mdiv = div( hdiv.rem, MINS );
  104.  
  105.         sprintf( strbuf, "     Time Spent: %02d:%02d:%02d/%ld\n\n",
  106.             hdiv.quot, mdiv.quot, mdiv.rem, total_seconds );
  107.         strcat( outline, strbuf );
  108.         fprintf( wrklog, outline );
  109.     }
  110.     /* Calculate the overall "time spent"   */
  111.     else if( strcmp( "CALC", argv[ 1 ] ) == 0 )
  112.     {
  113.         rewind( wrklog );
  114.         while( !feof( wrklog ) )
  115.         {
  116.             /* This is to eliminate garbage or residual entries.    */
  117.             outline[ 0 ] = '\0';
  118.  
  119.             fgets( outline, 80, wrklog );
  120.             if( strstr( outline, "OUT" ) != NULL )
  121.             {
  122.                 total_seconds += atol( &outline[ HOUR_OFF ] );
  123.             }
  124.  
  125.         }
  126.  
  127.         /* goto to end of file and stamp total hours    */
  128.         fastforw( wrklog );
  129.         if( total_seconds )
  130.         {
  131.             hdiv = div( total_seconds, HOUR );
  132.             mdiv = div( hdiv.rem, MINS );
  133.             fprintf( wrklog, "\t\t\t\t\t\t\t\t\t\t  "
  134.                 "Total Hours: %02d:%02d:%02d/%ld\n",
  135.                 hdiv.quot, mdiv.quot, mdiv.rem, total_seconds );
  136.         }
  137.     }
  138.     else
  139.     {
  140.         usage();
  141.         quit( INVALID );
  142.     }
  143.  
  144.     quit( OK );
  145. }
  146.  
  147. void usage( void )
  148. {
  149.     printf( "\nusage: LOG [IN][OUT][CALC]\n");
  150. }
  151.  
  152. /****************************************************************
  153. * get_in_entry - gets a previous IN entry.                      *
  154. *                                                               *
  155. *  Params: FILES *fp - file pointer.                            *
  156. * Returns: The entry's seconds if successful, else 0            *
  157. *                                                               *
  158. * NOTES: If fseek fails for any reason, function does not       *
  159. *        return. Instead, quit is call with the error code.     *
  160. ****************************************************************/
  161.  
  162. long get_in_entry( FILE *fp )
  163. {
  164.  
  165.     if( fseek( fp, -IN_ENTRY, SEEK_END ) != 0 )
  166.         quit( SEEK_ERROR );
  167.  
  168.     fread( strbuf, 1, IN_ENTRY, fp );
  169.     fastforw( fp );
  170.  
  171.     if( strstr( strbuf, "IN" ) == NULL )
  172.         return( 0 );
  173.     else
  174.     {
  175.         return( atol( &strbuf[ SEC_OFF ]));
  176.     }
  177. }
  178.  
  179. /****************************************************************
  180. * quit() - Program exit function. Reports of any outstanding    *
  181. *          errors.                                              *
  182. *                                                               *
  183. *  Params:  errcode - Error code as defined in beginning.       *
  184. * Returns:  nothing.                                            *
  185. ****************************************************************/
  186.  
  187. void quit( int errcode )
  188. {
  189.     char *errmsg[] =
  190.     {
  191.         "",
  192.         "Log has an open entry.",
  193.         "No corresponding IN entry.",
  194.         "File open error.",
  195.         "Seek error",
  196.         "No parameters specified.",
  197.         "Invalid Parameters."
  198.     };
  199.  
  200.     printf( "\n%s\n", errmsg[ errcode ] );
  201.  
  202.     fclose( wrklog );
  203.     exit( errcode );
  204. }
  205.  
  206. /****************************************************************
  207. * fastforw() - Puts file pointer to end of file.                *
  208. *                                                               *
  209. *  Params: fp - File pointer.                                   *
  210. * Returns: nothing.                                             *
  211. ****************************************************************/
  212.  
  213. void fastforw( FILE *fp )
  214. {
  215.     fseek( fp, 0, SEEK_END );
  216. }
  217.